package com.perfma.xtest.tool;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: junhui.si
 * @date: 2022-06-21 16:28
 **/
public class JmxMethodReplace {
    static final Pattern p = Pattern.compile("\\$\\{__CSVRead\\(([\\w\\W]*)\\)}");

    public static void main(String[] args) {
        Matcher m = p.matcher(content);
        StringBuffer sb = new StringBuffer();
        int matcher_start = 0;
        while (m.find(matcher_start)) {
            String key = m.group(0);
            System.out.println(key);
//            String pre = key.substring(0, key.indexOf("(") + 1);
//            String suffix = key.substring(key.indexOf(","));
//            String temp = key.substring(key.indexOf("("), key.indexOf(","));
//            String value = "${__CSVRead(" + "嘿嘿嘿" + suffix + ")}";
//            System.out.println(value);
//            m.appendReplacement(sb, value);
            matcher_start = m.end();
        }
//        m.appendTail(sb);
//        System.out.println(sb.toString());

//        parse();
    }

    public static void parse() {
        Map<String, String> data = new HashMap<>();
        data.put("name", "zhangs");
        data.put("code", "2222");
        String content = "恭喜${name}报名成功，请凭报名编号${code}到现场参加活动";
        String pattern = "\\$\\{(.+?)\\}";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(content);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String key = m.group(1);
            String value = data.get(key);
            m.appendReplacement(sb, value == null ? "" : value);
        }
        m.appendTail(sb);
        System.out.println(sb.toString());
    }

    static String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<jmeterTestPlan version=\"1.2\" properties=\"5.0\" jmeter=\"5.4\">\n" +
            "  <hashTree>\n" +
            "    <TestPlan guiclass=\"TestPlanGui\" testclass=\"TestPlan\" testname=\"文件函数替换路径\" enabled=\"true\">\n" +
            "      <stringProp name=\"TestPlan.comments\"></stringProp>\n" +
            "      <boolProp name=\"TestPlan.functional_mode\">false</boolProp>\n" +
            "      <boolProp name=\"TestPlan.tearDown_on_shutdown\">true</boolProp>\n" +
            "      <boolProp name=\"TestPlan.serialize_threadgroups\">false</boolProp>\n" +
            "      <elementProp name=\"TestPlan.user_defined_variables\" elementType=\"Arguments\" guiclass=\"ArgumentsPanel\" testclass=\"Arguments\" testname=\"用户定义的变量\" enabled=\"true\">\n" +
            "        <collectionProp name=\"Arguments.arguments\"/>\n" +
            "      </elementProp>\n" +
            "      <stringProp name=\"TestPlan.user_define_classpath\"></stringProp>\n" +
            "    </TestPlan>\n" +
            "    <hashTree>\n" +
            "      <ThreadGroup guiclass=\"ThreadGroupGui\" testclass=\"ThreadGroup\" testname=\"线程组\" enabled=\"true\">\n" +
            "        <stringProp name=\"ThreadGroup.on_sample_error\">continue</stringProp>\n" +
            "        <elementProp name=\"ThreadGroup.main_controller\" elementType=\"LoopController\" guiclass=\"LoopControlPanel\" testclass=\"LoopController\" testname=\"循环控制器\" enabled=\"true\">\n" +
            "          <boolProp name=\"LoopController.continue_forever\">false</boolProp>\n" +
            "          <stringProp name=\"LoopController.loops\">1</stringProp>\n" +
            "        </elementProp>\n" +
            "        <stringProp name=\"ThreadGroup.num_threads\">1</stringProp>\n" +
            "        <stringProp name=\"ThreadGroup.ramp_time\">1</stringProp>\n" +
            "        <boolProp name=\"ThreadGroup.scheduler\">false</boolProp>\n" +
            "        <stringProp name=\"ThreadGroup.duration\"></stringProp>\n" +
            "        <stringProp name=\"ThreadGroup.delay\"></stringProp>\n" +
            "        <boolProp name=\"ThreadGroup.same_user_on_next_iteration\">true</boolProp>\n" +
            "      </ThreadGroup>\n" +
            "      <hashTree>\n" +
            "        <CSVDataSet guiclass=\"TestBeanGUI\" testclass=\"CSVDataSet\" testname=\"CSV 数据文件设置\" enabled=\"true\">\n" +
            "          <stringProp name=\"delimiter\">,</stringProp>\n" +
            "          <stringProp name=\"fileEncoding\"></stringProp>\n" +
            "          <stringProp name=\"filename\">/Users/sijunhui/Downloads/1-数据工厂/order-id.csv</stringProp>\n" +
            "          <boolProp name=\"ignoreFirstLine\">false</boolProp>\n" +
            "          <boolProp name=\"quotedData\">false</boolProp>\n" +
            "          <boolProp name=\"recycle\">true</boolProp>\n" +
            "          <stringProp name=\"shareMode\">shareMode.all</stringProp>\n" +
            "          <boolProp name=\"stopThread\">false</boolProp>\n" +
            "          <stringProp name=\"variableNames\"></stringProp>\n" +
            "        </CSVDataSet>\n" +
            "        <hashTree/>\n" +
            "        <TransactionController guiclass=\"TransactionControllerGui\" testclass=\"TransactionController\" testname=\"事务控制器\" enabled=\"true\">\n" +
            "          <boolProp name=\"TransactionController.includeTimers\">false</boolProp>\n" +
            "          <boolProp name=\"TransactionController.parent\">false</boolProp>\n" +
            "        </TransactionController>\n" +
            "        <hashTree/>\n" +
            "        <BeanShellSampler guiclass=\"BeanShellSamplerGui\" testclass=\"BeanShellSampler\" testname=\"BeanShell 取样器\" enabled=\"true\">\n" +
            "          <stringProp name=\"BeanShellSampler.query\">${__CSVRead(a1,a2)} ${__CSVRead(a2,a3)}\n" +
            "${__CSVRead(a1\n" +
            ",a2)}\n" +
            "${__CSVRead(a1,\n" +
            "a2)}\n" +
            "hhhh</stringProp>\n" +
            "          <stringProp name=\"BeanShellSampler.filename\"></stringProp>\n" +
            "          <stringProp name=\"BeanShellSampler.parameters\"></stringProp>\n" +
            "          <boolProp name=\"BeanShellSampler.resetInterpreter\">false</boolProp>\n" +
            "        </BeanShellSampler>\n" +
            "        <hashTree/>\n" +
            "      </hashTree>\n" +
            "    </hashTree>\n" +
            "  </hashTree>\n" +
            "</jmeterTestPlan>\n";
}
